package com.netease.framework.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.netease.framework.db.DBSupport.PreferenceTable;

/**
 * 保存简单key value对
 * warning: 为避免key冲突, 最好统一能统一定义key
 * 
 * @author Panjf
 */
public class Preference{
	private static final String DEFAULT_BELONG = "default_belong";
	
	private static final String TRUE = "true";
	private static final String FALSE = "false";
	
	private static String getSelection(String belong, String key){
		StringBuffer select = new StringBuffer();
		if(key == null){
			throw new NullPointerException("key can not be null...");
		}
		select.append(PreferenceTable.KEY);
		select.append(" = '");
		select.append(key);
		select.append("' ");
		
		if(belong == null)
			belong = DEFAULT_BELONG;
		
		select.append(" AND ");
		select.append(PreferenceTable.BELONGTO);
		select.append(" = '");
		select.append(belong);
		select.append("' ");			
		
		return select.toString();
	}
	
	private static Cursor query(Context context, String belong, String key){
		String select = getSelection(belong, key);
		
		return context.getContentResolver().query(PreferenceTable.Preference_URI, 
		                null, select, null, null);
	}
	
	
	public static void setValue(Context context, String belong, String key, String value){
		ContentValues values = new ContentValues();
		values.put(PreferenceTable.VALUE, value);
		
		Cursor c = query(context, belong, key);
		if(c != null && c.getCount() > 0){
			context.getContentResolver().update(PreferenceTable.Preference_URI, values, 
						getSelection(belong, key), null);
		}
		else{
			values.put(PreferenceTable.KEY, key);
			values.put(PreferenceTable.BELONGTO, belong == null ? DEFAULT_BELONG : belong);
			context.getContentResolver().insert(PreferenceTable.Preference_URI, values);
		}
		
		if(c != null)
			c.close();
	}
	
	public static void setValue(Context context, String belong, String key, int value){
		String s = String.valueOf(value);
		setValue(context, belong, key, s);
	}
	
	public static void setValue(Context context, String belong, String key, long value){
		String s = String.valueOf(value);
		setValue(context, belong, key, s);
	}

	public static void setValue(Context context, String belong, String key, float value){
		String s = String.valueOf(value);
		setValue(context, belong, key, s);
	}
	
	
	public static void setValue(Context context, String belong, String key, boolean value){
		String s;
		if(value){
			s = TRUE;
		}
		else{
			s = FALSE;
		}
		setValue(context, belong, key, s);
	}
	
	public static String getValue(Context context, String belong, String key, String defaultValue){
		String value = defaultValue;
		
		Cursor c = query(context, belong, key);
		if(c != null && c.getCount() > 0){
			c.moveToFirst();
			value = c.getString(c.getColumnIndex(PreferenceTable.VALUE));
		}
		
		if(c != null)
			c.close();
		
		return value;
	}
	
	public static int getValue(Context context, String belong, String key, int defaultValue){
		String value = getValue(context, belong, key, null);
		if(value != null){
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException e) {
				
			}
		}
		
		return defaultValue;
	}
	
	public static long getValue(Context context, String belong, String key, long defaultValue){
		String value = getValue(context, belong, key, null);
		if(value != null){
			try {
				return Long.parseLong(value);
			} catch (NumberFormatException e) {
				
			}
		}
		
		return defaultValue;
	}
	
	public static float getValue(Context context, String belong, String key, float defaultValue){
		String value = getValue(context, belong, key, null);
		if(value != null){
			try {
				return Float.parseFloat(value);
			} catch (NumberFormatException e) {
				
			}
		}
		
		return defaultValue;
	}
	
	public static boolean getValue(Context context, String belong, String key, boolean defaultValue){
		String value = getValue(context, belong, key, null);
		
		if(value != null){
			if(value.equals(TRUE))
				return true;
			else if(value.equals(FALSE))
				return false;
		}
		
		return defaultValue;
	}
	
	public static void unSetValue(Context context, String belong, String key){
		context.getContentResolver().delete(PreferenceTable.Preference_URI, 
				getSelection(belong, key), null);
	}
}
